<?php


namespace app\index\controller;

class System extends \app\common\Controller
{
	public function get_website_info()
	{
		$info = \app\common\server\ConfigServer::get("website");
		return json(["code" => 1, "msg" => "获取成功", "data" => $info]);
	}
	public function get_custom_image()
	{
		$file = \think\facade\Env::get("root_path") . "public/" . base64_decode(input("name"));
		if (empty($file) || !is_file($file)) {
			return json(["code" => -1, "msg" => "图片不存在"]);
		}
		if (!$this->isImage($file)) {
			return json(["code" => -1, "msg" => "非法文件"]);
		}
		$height = input("height");
		$width = input("width");
		$type = input("type", 1);
		$quality = input("quality", 80);
		if (!empty($height) && !empty($width)) {
			$image = \think\Image::open($file);
			$dirs = dirname($file);
			$savepath = $dirs . "/" . basename($file) . "_" . $width . "_" . $height . "_" . $quality . "." . $image->type();
			if (is_file($savepath)) {
				$flow = file_get_contents($savepath);
			} else {
				$image->thumb($height, $width, $type)->save($savepath, null, $quality);
				$image = \think\Image::open($savepath);
				$flow = file_get_contents($savepath);
			}
		} else {
			$image = \think\Image::open($file);
			$flow = file_get_contents($file);
		}
		header("Content-type: image/" . $image->type());
		echo $flow;
		exit;
	}
	public function isImage($imgPath)
	{
		if (file_exists($imgPath)) {
			if (!($info = getimagesize($imgPath))) {
				return 0;
			}
			$ext = image_type_to_extension($info[2]);
			return in_array($ext, [".gif", ".jpeg", ".png", ".bmp", ".jpg"]);
		} else {
			return false;
		}
	}
	public function data_export_file_download()
	{
		$get = input();
		if (isset($get["id"]) && isset($get["token"])) {
			$log = \think\Db::name("data_export_file")->where(["id" => $get["id"]])->find();
			if (!empty($log)) {
				if ($log["expire_time"] < time()) {
					return json(["code" => -1, "msg" => "链接过期"]);
				}
				if (md5($log["id"] . $log["key"]) != $get["token"]) {
					return json(["code" => -1, "msg" => "链接错误"]);
				}
				if (!is_file($log["path"])) {
					return json(["code" => -1, "msg" => "源文件丢失无法下载"]);
				}
				$file = fopen($log["path"], "rb");
				Header("Content-type: application/octet-stream");
				Header("Accept-Ranges: bytes");
				Header("Accept-Length: " . filesize($log["path"]));
				Header("Content-Disposition: attachment; filename=" . $log["name"]);
				echo fread($file, filesize($log["path"]));
				fclose($file);
				exit;
			} else {
				return json(["code" => -1, "msg" => "无记录"]);
			}
		}
	}
	public function get_system_product_info()
	{
		$root_path = \think\facade\Env::get("root_path");
		if (!is_file($root_path . "system/config/version.php")) {
			return json(["code" => -1, "msg" => "检测到系统配置文件缺失"]);
		}
		$system_version_config = (include $root_path . "system/config/version.php");
		$system = new \app\common\system\System();
		$system_product_config = $system->getProductInfo();
		$data = ["version" => $system_version_config, "product" => $system_product_config, "auths_info" => $system->getAuthInfo()];
		return json(["code" => 1, "msg" => "获取成功", "data" => $data]);
	}
}